tool ?= verilator

VLOG_INC_DIR = -I../include/
VLOG_INC = $(wildcard ../include/*.vh)
VLOG_SRC = $(wildcard ../src/*.v) $(wildcard ../mem_model/*.v) ../tb/licore_tb.v

ifeq ($(tool), iverilog)
VLOG_SRC += ../tb/sim_ivl.v
else ifeq ($(tool), verilator)
VLOG_SRC += ../tb/sim_main.cpp
endif

ifeq ($(tool), iverilog)
  EXE = sim.out
  GTKW_CONFIG = ivl_config.gtkw
else ifeq ($(tool), verilator)
  EXE = obj_dir/Vlicore_tb
  VERILATOR_OPT = -O2 --trace --build-jobs 4
  GTKW_CONFIG = vla_config.gtkw
else
  EXE = exe
endif

build: $(EXE)

run: sim.wave

view: run $(GTKW_CONFIG)
	gtkwave --save $(GTKW_CONFIG) --optimize sim.wave

clean:
	@rm -rf *.out *.fst *.wave
	@rm -rf obj_dir/

.PHONY: view clean

$(EXE): $(VLOG_INC) $(VLOG_SRC)
ifeq ($(tool), iverilog)
	iverilog $(VLOG_INC_DIR) $(VLOG_SRC) -o $(EXE)
else ifeq ($(tool), verilator)
	verilator --cc --build --exe $(VERILATOR_OPT) --top licore_tb $(VLOG_INC_DIR) $(VLOG_SRC) 
else
	@echo "Unkown tool: $(tool)" && exit 1
endif

sim.wave: $(EXE) licore.rom
ifeq ($(tool), iverilog)
	vvp $(EXE)
else ifeq ($(tool), verilator)
	./$(EXE)
endif

